package my.download;

import org.apache.hc.client5.http.async.methods.*;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.support.ClassicRequestBuilder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class HttpClientTest1 {

    public static void main(String[] args) {
        test1();
    }

    // 715583
    private static void test1() {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(100, 200,
                1L, TimeUnit.HOURS, new ArrayBlockingQueue<>(5000),new ThreadPoolExecutor.CallerRunsPolicy());
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            ClassicHttpRequest loginReq = ClassicRequestBuilder.post("http://121.41.39.119/index.html")
                    .build();
            long start = System.currentTimeMillis();
            CountDownLatch countDownLatch = new CountDownLatch(200000);
            for (int i = 0; i < 200000; i++) {
                executor.execute(() -> {
                    try {
                        String execute = httpClient.execute(loginReq, entity -> EntityUtils.toString(entity.getEntity()));
                        System.out.println("success");
                    } catch (IOException e) {
                        System.out.println("fail");
                    } finally {
                        countDownLatch.countDown();
                    }
                });
            }
            countDownLatch.await();
            System.out.println("耗时：" + (System.currentTimeMillis() - start));
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private static void test2() {
        try (CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault()) {
            httpClient.start();
            SimpleHttpRequest httpRequest = SimpleRequestBuilder.get("http://121.41.39.119/index.html").build();
            long start = System.currentTimeMillis();
            int count = 200000;
            CountDownLatch countDownLatch = new CountDownLatch(count);
            Semaphore semaphore = new Semaphore(1000);
            for (int i = 0; i < count; i++) {
                semaphore.acquire();
                    httpClient.execute(httpRequest,
                            new FutureCallback<SimpleHttpResponse>() {
                                @Override
                                public void completed(SimpleHttpResponse simpleHttpResponse) {
                                    semaphore.release();
                                    countDownLatch.countDown();
//                        System.out.println(simpleHttpResponse.getBodyText());
                                    System.out.println("success1");
                                }

                                @Override
                                public void failed(Exception e) {
                                    semaphore.release();
                                    countDownLatch.countDown();
                                    System.out.println("fail");
                                }

                                @Override
                                public void cancelled() {
                                    semaphore.release();
                                    countDownLatch.countDown();
                                    System.out.println("cancel");
                                }
                            });
            }
            countDownLatch.await();
            System.out.println("耗时：" + (System.currentTimeMillis() - start));
        } catch (IOException e) {
            System.out.println(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
